package com.aaa.shuke2024.utils;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import org.thymeleaf.util.StringUtils;

/**
 * @author ：Teacher陈（86521760@qq.com）
 * @date ：Created in 2024/12/2 18:45
 * @description：我的工具
 * @modified By：
 * @version: 1.0
 */
public class MyTools {
    /**
     * @create by: Teacher陈
     * @description: 输入明文和盐值，返回密文
     * @create time: 2024/12/2 18:46
     * @param password
     * @param salt
     * @return  hex
     */
    public static   String makePassword(String password,String salt){
        String hex = SecureUtil.hmacMd5(password + salt).digestHex(salt);
        return hex;
    }
    /**
     * @create by: Teacher陈
     * @description: 获取盐值，随机代码
     * @create time: 2024/12/2 18:50
     * @return code
     */
    public static String getSalt(){
        return RandomUtil.randomString(11);
    }


    /**
     * @create by: Teacher陈
     * @description: 校验密码是否正确，将明文密码重新加密，使用之前的盐值，看密文是否一样
     * @create time: 2024/12/2 18:55
     * @param password
     * @param salt
     * @param dbpassword
     * @return  boolean
     */
    public static boolean checkPassword(String password,String salt ,String dbpassword){
        String makePassword = makePassword(password, salt);
        Boolean equals = StringUtils.equals(makePassword, dbpassword);
        return equals;

    }

    public static void main(String[] args) {
      /*  String salt=getSalt();
        String password = MyTools.makePassword("chenjian", salt);
        System.out.println("盐值："+salt);
        System.out.println("密文："+password);*/
        boolean b = checkPassword("chenjian", "hxh2jfvEBU6", "0931654ad8095400ddf9db24db095c9d");
        System.out.println(b);
    }
}
